import { mapServer } from "../server";

class LineString {
    /**
     * 添加线要素
     * @param {Object} options
     * @param {Array} options.coordinates 线的坐标点数组，[[x1,y1],[x2,y2],...]
     * @param {Array} options.attr 线要素的属性数组，[{name:"name",value:"value",type:"string"}]
     * @param {Object} options.service 地图服务对象
     * @param {Object} options.docLayer 地图文档图层对象
     */

    static add({
                   coordinates,
                   attr = [],
                   service,
                   docLayer,
               }) {
        //构成线要素的点
        var pointObj = new Array();

        //[x,y] => new Zondy.Object.Point2D(x, y);
        coordinates.forEach(function (item) {
            var point = new Zondy.Object.Point2D(item[0], item[1]);
            pointObj.push(point);
        });
        // console.log(pointObj, 'pointObj')

        //构成折线的弧段
        var gArc = new Zondy.Object.Arc(pointObj);
        //构成线的折线
        var gAnyLine = new Zondy.Object.AnyLine([gArc]);
        //设置线要素的几何信息
        var gline = new Zondy.Object.GLine(gAnyLine);
        //设置要素的几何信息
        var fGeom = new Zondy.Object.FeatureGeometry({LinGeom: [gline]});
        //随机输出1~8之间的整数,作为新添加的要素的颜色号
        var lineColor = 8;

        //设置添加线要素的图形参数信息
        var clineInfo = new Zondy.Object.CLineInfo({
            "Color": lineColor,
            "LinStyleID": 17,//线型编号
            "LinStyleID2": 1,
            "LinWidth": 0.05,
            "Xscale": 10,
            "Yscale": 10
        });
        //设置要素的图形参数信息
        var graphicInfo = new Zondy.Object.WebGraphicsInfo(
            {
                InfoType: 2,
                LinInfo: clineInfo
            }
        );

        //设置添加线要素的属性信息
        var attValue = attr.map((item) => item.value);

        //创建一个线要素
        var newFeature = new Zondy.Object.Feature(
            {
                fGeom: fGeom,
                GraphicInfo: graphicInfo,
                AttValue: attValue
            }
        );
        //设置要素为线要素
        newFeature.setFType(2);

        //创建一个要素数据集
        var featureSet = new Zondy.Object.FeatureSet();
        var fldNumber = attr.length; //属性个数
        var fldName = attr.map((item) => item.name);
        var fldType = attr.map((item) => item.type);
        //创建属性结构设置对象
        var cAttStruct = new Zondy.Object.CAttStruct(
            {
                FldName: fldName,
                FldNumber: fldNumber,
                FldType: fldType
            }
        );
        //设置要素数据集的树形结构
        featureSet.AttStruct = cAttStruct;
        //将添加的线要素添加到属性数据集中
        featureSet.addFeature(newFeature);

        console.log(featureSet, 'featureSet')

        //创建一个地图编辑对象
        var editDocFeature = new Zondy.Service.EditDocFeature(service.docName, service.layerId, mapServer);
        editDocFeature.add(featureSet, this.onLineSuccess(docLayer));
    }

    /**
     * 更新线要素
     * @param {Object} options
     * @param {Array} options.coordinates 线的坐标点数组，[[x1,y1],[x2,y2],...]
     * @param {Array} options.attr 线要素的属性数组，[{name:"name",value:"value",type:"string"}]
     * @param {Object} options.service 地图服务对象
     * @param {Object} options.docLayer 地图文档图层对象
     */
    static update({coordinates, attr, service, docLayer}) {
        console.log(coordinates, 'coordinates');
        console.log(attr, 'attr');
        console.log(service,'service');
        console.log(docLayer, 'docLayer');
        //要修改的线要素的ID
        const featureIds = [1];

        /* 2.1 构建几何信息 */
        var pointObj = [];
        coordinates.forEach(function (item) {
          var point = new Zondy.Object.Point2D(item[0], item[1]);
          pointObj.push(point);
        });
        /* 构成折线的弧度 */
        var gArc = new Zondy.Object.Arc(pointObj);
        /* 构成线的折线 */
        var gAnyLine = new Zondy.Object.AnyLine([gArc]);
        /* 设置线要素的几何信息 */
        var gLine = new Zondy.Object.GLine(gAnyLine);
        /* 设置要素的几何信息 */
        var fGeom = new Zondy.Object.FeatureGeometry({ LinGeom: [gLine] });
        /* 2.2 设置而样式信息 */
        //设置添加线要素的图形参数信息
        var clineInfo = new Zondy.Object.CLineInfo({
          Color: 1, //子图的颜色号
          LinStyleID: 0,
          LinStyleID2: 19,
          LinWidth: 0.1, //线宽
          Xscale: 10,
          Yscale: 10,
        });
        //设置要素的图形参数
        var graphicInfo = new Zondy.Object.WebGraphicsInfo({
          InfoType: 2, //线
          LinInfo: clineInfo,
        });
        /* 2.3 设置属性信息 */
        var attValue = attr.map((item)=>item.value);
        console.log(attValue, 'attValue');
        /* 2.4  构建要素类*/
        //创建一个线要素
        var newFeature = new Zondy.Object.Feature({
          fGeom: fGeom,
          GraphicInfo: graphicInfo,
          AttValue: attValue,
        });
        //设置要为线要素
        newFeature.setFType(2);
        newFeature.setFID(2);
        /* 2.5 设置要素集 */
        //创建一个要素数据集
        var featureSet = new Zondy.Object.FeatureSet();
        //创建属性结构设置对象
        var cAttStruct = new Zondy.Object.CAttStruct({
          FldName:attr.map((item)=>item.name),
          FldNumber: attr.length,
          FldType: attr.map((item)=>item.type),
        });
        console.log(cAttStruct, 'cAttStruct');
        //设置要素数据集的属性结构
        featureSet.AttStruct = cAttStruct;
        //将添加的线要素添加到属性数据集中
        featureSet.addFeature(newFeature);
        /* 2.6 调用地图编辑服务接口 */
        //创建一个地图编辑对象
        var editDocFeature = new Zondy.Service.EditDocFeature(service.docName, service.layerId, mapServer);
        editDocFeature.update(featureSet, this.onLineSuccess(docLayer));
      }

    //添加线要素回调函数
    static onLineSuccess(docLayer) {
        return function (rlt) {
            var result = rlt;
            if (result) {
                alert("操作线要素成功！");
                docLayer.refresh();
            } else {
                alert("操作线要素失败！");
            }
        }
    }
}